home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_02 / ED-157 / get_offset.c < prev    next >
C/C++ Source or Header  |  1993-09-10  |  2KB  |  69 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record (rhr@clio.rice.edu)
  3.  * 
  4.  * This file is part of ED.
  5.  * 
  6.  * ED is free software; you can redistribute it and/or modify it under the terms
  7.  * of the GNU General Public License as published by the Free Software Foundation.
  8.  * 
  9.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  12.  * 
  13.  * You should have received a copy of the GNU General Public License along with ED
  14.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  15.  * Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. #include "opsys.h"
  18.  
  19. #include "rec.h"
  20. #include "window.h"
  21. #include "ed_dec.h"
  22.  
  23. /******************************************************************************\
  24. |Routine: get_offset
  25. |Callby: edit output_file sort_recs trim word_fill
  26. |Purpose: Calculates the offset from the current position to a spot in the file.
  27. |Arguments:
  28. |    rec is the record containing the spot.
  29. |    byt is the byte offset in that record.
  30. |    dir is the direction from the cursor to the spot.
  31. \******************************************************************************/
  32. Int get_offset(rec,byt,dir)
  33. rec_ptr rec;
  34. Int byt,dir;
  35. {
  36.     register rec_ptr r;
  37.     register Int offset,b;
  38.  
  39.     r = CURREC;
  40.     b = CURBYT;
  41.     if(r == rec && b == byt)
  42.         return(0);
  43.     if(r == rec)
  44.         return(byt - b);
  45.     offset = 0;
  46.     if(dir >= 0)
  47.     {
  48.         while(r != rec)
  49.         {
  50.             offset += r->length - b + 1;
  51.             b = 0;
  52.             r = r->next;
  53.         }
  54.         return(offset + byt);
  55.     }
  56.     else
  57.     {
  58.         b = r->length - b;
  59.         while(r != rec)
  60.         {
  61.             offset += r->length - b + 1;
  62.             b = 0;
  63.             r = r->prev;
  64.         }
  65.         return(-(offset + r->length - byt));
  66.     }
  67. }
  68.  
  69.